package cn.snmp.mutual.adapter.impl;


import cn.snmp.mutual.nms.SnmpString;
import cn.snmp.mutual.nms.SnmpVar;
import cn.snmp.mutual.adapter.GeneralMibNodeAdapter;
import cn.snmp.mutual.data.ObjService;
import lombok.extern.slf4j.Slf4j;

/**
 * @author dengkn
 *
 *
 * 测试mib设配处理
 */
@Slf4j
public class TestModeMibNodeAdapter extends GeneralMibNodeAdapter {

    @Override
    public String readMibData(SnmpVar var, ObjService mibnode) {
        String syntaxName = mibnode.getStringValue("SyntaxName");
        String label = mibnode.getStringValue("Label");

        log.debug("[" + label + "] Name is [" + syntaxName + "], Syntax is [" + var.getTypeString() + "]");
        byte[] bytes = var.toBytes();
        int cir = (bytes[0] & (1 << 7)) >> 7;
        int eir = (bytes[0] & (1 << 6)) >> 6;
        int overload = (bytes[0] & (1 << 5)) >> 5;

        StringBuffer sbValue = new StringBuffer();
        sbValue.append("CIR:").append(String.valueOf(cir));
        sbValue.append(";EIR:").append(String.valueOf(eir));
        sbValue.append(";OVERLOAD:").append(String.valueOf(overload));
        return sbValue.toString();
    }

    @Override
    public SnmpVar writeMibData(String value, ObjService mibnode) {
        String syntaxName = mibnode.getStringValue("SyntaxName");
        String label = mibnode.getStringValue("Label");
        log.debug("[" + label + "] Name is [" + syntaxName + "], Value is [" + value + "]");
        SnmpVar var = null;
        char c1val = 'n';//
        char c2val = 'n';// EIR
        char c3val = 'n';// CIR
        if (value.length() == 1) {
            c3val = value.charAt(0);// CIR
        } else {
            c1val = value.charAt(2);//OverLoad
            c2val = value.charAt(1);// EIR
            c3val = value.charAt(0);// CIR
        }

        byte[] bytes = new byte[1];

        if (c1val == '1') {
            bytes[0] |= 1 << 5; // OverLoad
        }


        if (c2val == '1') {
            bytes[0] |= 1 << 6; // EIR
        }

        if (c3val == '1') {
            bytes[0] |= 1 << 7; // CIR
        }
        var = (SnmpVar) new SnmpString(bytes);

        return var;
    }


}
